﻿using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Dmo_lc_2.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dmo_lc_2.Shape
{
    class MlineShape : BaseShape
    {
        public int justification;
        public double scale;

        public bool isClosed;
        public bool supressStartCaps;
        public bool supressEndCaps;
        public int numberOfVertices;

        public MlineStyleData mlineStyleData;
        public List<double[]> vertices = new List<double[]>();


        public MlineShape(Entity in_entity, Transaction tr) : base(in_entity, tr)
        {
        }

        public override void Init()
        {
            Mline in_mline = (Mline)mEntity;
            ObjectId lineTypeId = in_mline.LinetypeId;
            shapeType = ShapeType.MLINE;
            shapeTypeName = shapeType.ToString();

            justification = (int)in_mline.Justification;
            scale = in_mline.Scale;
            isClosed = in_mline.IsClosed;
            supressStartCaps = in_mline.SupressStartCaps;
            supressEndCaps = in_mline.SupressEndCaps;
            numberOfVertices = in_mline.NumberOfVertices;

            for (int i = 0; i < numberOfVertices; i++) {
                Point3d pos = in_mline.VertexAt(i);
                vertices.Add(new double[] { pos.X, pos.Y, pos.Z });
            }
            
            ObjectId styleId = in_mline.Style;
            MlineStyle mlineStyle = mTransaction.GetObject(styleId, OpenMode.ForRead) as MlineStyle;
            mlineStyleData = genMlineStyle(mlineStyle , mLayer.ObjectId);
            mlineStyle.ToString();
        }

        MlineStyleData genMlineStyle( MlineStyle in_style , ObjectId layerId) {
            MlineStyleData reData = new MlineStyleData();
            reData.showMiters = in_style.ShowMiters;
            reData.startSquareCap = in_style.StartSquareCap;
            reData.startRoundCap = in_style.StartRoundCap;
            reData.startInnerArcs = in_style.StartInnerArcs;

            reData.endSquareCap = in_style.EndSquareCap;
            reData.endRoundCap = in_style.EndRoundCap;
            reData.endInnerArcs = in_style.EndInnerArcs;
            reData.fillColor = new int[] { in_style.FillColor.ColorValue.R,
                in_style.FillColor.ColorValue.G, in_style.FillColor.ColorValue.B };

            reData.filled = in_style.Filled;
            reData.startAngle = in_style.StartAngle;
            reData.endAngle = in_style.EndAngle;

            reData.name = in_style.Name;
            reData.description = in_style.Description;

            int count = in_style.Elements.Count;
            for (int i = 0; i < count; i++) {
                // reData.mlineStyles.Add(in_style.Elements[i]);
                MlineStyleElement styleElement = in_style.Elements[i];

                MlineStyleItem mlineStyleItem = new MlineStyleItem();
                mlineStyleItem.offset = styleElement.Offset;

                if (styleElement.Color.ColorMethod == ColorMethod.ByLayer)
                    mlineStyleItem.color = getLayerData.getLayerColor(mTransaction , layerId);
                else 
                    mlineStyleItem.color = new int[] { styleElement.Color.ColorValue.R,
                        styleElement.Color.ColorValue.G, styleElement.Color.ColorValue.B };

                mlineStyleItem.lineTypeList = getLineTypeData.getLineTypeDataById(mTransaction , 
                    styleElement.LinetypeId);

                mlineStyleItem.lineTypeList = getLineType(styleElement.LinetypeId, 
                    mTransaction,mEntity.LayerId, mEntity.BlockId);

                reData.mlineStyles.Add(mlineStyleItem);
            }
            return reData;
        }
    }

    public class MlineStyleData {
        public bool showMiters { get; set; }
        public bool startSquareCap { get; set; }
        public bool startRoundCap { get; set; }
        public bool startInnerArcs { get; set; }
        public bool endSquareCap { get; set; }
        public bool endRoundCap { get; set; }
        public bool endInnerArcs { get; set; }
        public int[] fillColor { get; set; }
        public bool filled { get; set; }
        public double startAngle { get; set; }
        public double endAngle { get; set; }

        public List<MlineStyleItem> mlineStyles = new List<MlineStyleItem>();

        public string name { get; set; }
        public string description { get; set; }
    }


    public class MlineStyleItem
    {
        public List<Dictionary<string, object>> lineTypeList;
        public int[] color { get; set; }
        public double offset { get; set; }
    }
}
